package libraries;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import testScripts.TestDriver;

public class KeyWordLib extends TestDriver 
{
	//@Test(description="Enters input")
	//public static EventFiringWebDriver driver = null;
	public static String  mySQLDB, myDriver, myT1, myUName, myPswd;
	public static String myQuery1, myQuery2;
	public static Connection myCon = null;
	public static Statement mySt;
	public static PreparedStatement myPst;
	public static ResultSet rs1, rs2;
	public static String p1, p2, r1, vDB_Result;
	
	
	public static String Enter_Input(){
		System.out.println("This is Enter Input method");
		try {
			driver.findElement(By.xpath(OBJDESC.getProperty(object))).click();
			driver.findElement(By.xpath(OBJDESC.getProperty(object))).clear();
			driver.findElement(By.xpath(OBJDESC.getProperty(object))).sendKeys(data);
			driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
			driver.findElement(By.xpath(OBJDESC.getProperty(object))).sendKeys(Keys.ENTER);
			Thread.sleep(2000);
			return "Pass";
		} 
		catch (Throwable t) {
			System.out.println("Object " + object + " Not found "+ t.getMessage());
			return "Fail" + t.getMessage();
		}

	}

	public static String Get_Text(){
		System.out.println("This is Get Text method");
		try {
			
			WebElement element = driver.findElement(By.xpath(OBJDESC.getProperty(object)));
			String strng = element.getText();
			System.out.println(strng);
			return strng;
		} catch (Throwable t) {
			System.out.println("Object " + object + " Not found "+ t.getMessage());
			return "Fail" + t.getMessage();
		}

	}
	
	public static String Verify_Text(){
		System.out.println("This is Verify Text method");
		System.out.println(object);
		try {
			WebElement element = driver.findElement(By.xpath(OBJDESC.getProperty(object)));
			String strng = element.getText();
			System.out.println(strng);
			System.out.println(data);
			
			if (data.equals("NA") )
			 { 	
				data = "";
			 System.out.println(data);
			 };
			
			//  Assert.assertEquals(data, strng);
			if (strng.equals(data)) {
				System.out.println("Message match");
				return "Pass";
			} else {
				System.out.println("Message not matching");
				return "Fail";
			}
			
		} catch (Throwable t) {
			System.out.println("Object " + object + " Not found "+ t.getMessage());
			return "Fail" + t.getMessage();
		}
	}
	
	public static String CheckIn(){
		System.out.println("This is checkIn method");
		try {
			driver.findElement(By.xpath("//span[@class='t-link']")).click();
			Thread.sleep(2000);
			driver.findElement(By.xpath("//html/body/div/div[1]/table/tbody/tr/td[1]/div/ul/li[2]/div/ul/li[2]")).click();
			driver.findElement(By.xpath("//*[@id='CheckInSearch_InboundTrackingNumber']")).sendKeys(data);
			driver.findElement(By.xpath("//input[@value='Search']")).click();
			driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
			//Thread.sleep(60000);
			//Wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@value='Create New LP']")));
			return "Pass";
		} catch (Throwable t) {
			System.out.println("Object " + object + " Not found "+ t.getMessage());
			return "Fail" + t.getMessage();
		}
	}

	public static String enter_dimension(){
		System.out.println("This is enter_dimension method");
		try {
			driver.findElement(By.id("Parcel.Length")).clear();
			driver.findElement(By.id("Parcel.Length")).sendKeys(vLength);
			driver.findElement(By.id("Parcel.Width")).clear();
			driver.findElement(By.id("Parcel.Width")).sendKeys(vWidth);
			driver.findElement(By.id("Parcel.Height")).clear();
			driver.findElement(By.id("Parcel.Height")).sendKeys(vHeight);
			driver.findElement(By.id("Parcel.WeightTotal")).clear();
			driver.findElement(By.id("Parcel.WeightTotal")).sendKeys(vWeight);
			return "Pass";
		} catch (Throwable t) {
			//System.out.println("Length is " + vLength);
			System.out.println("Object " + object + " Not found "+ t.getMessage());
			return "Fail" + t.getMessage();
		}
	}

	public static String Click(){
		System.out.println("This is click method");
		try {
			
			WebDriverWait wait = new WebDriverWait(driver, 15);		
			wait.until(ExpectedConditions.elementToBeClickable(By.xpath(OBJDESC.getProperty(object))));
			driver.findElement(By.xpath(OBJDESC.getProperty(object))).click();
			return "Pass";
		} catch (Throwable t) {
			System.out.println("Object " + object + " Not found "+ t.getMessage());
			return "Fail" + t.getMessage();
		}
	}
	

	
	public static String SelectDropDown(){
		System.out.println("This is SelectDropDown method");
		try {
		     
			 Select drpdwn = new Select(driver.findElement(By.xpath(OBJDESC.getProperty(object))));
			  drpdwn.selectByVisibleText(data);
			  Thread.sleep(1000);
			return "Pass";
		} catch (Throwable t) {
			System.out.println("Object " + object + " Not found " + t.getMessage());
			return "Fail" + t.getMessage();
		}
	}
	
	public static String SwitchtoPopUP(){
		System.out.println("This is SwitchtoPopUP method");
		try {
			
			Thread.sleep(1000);			
			// Window Handles when more than 1 Browser are present
			Set<String> winId  = driver.getWindowHandles();			
			Iterator<String> winIds = winId.iterator();
			List<String> widL = new ArrayList();
			System.out.println("Size of the window hanldes(ID) is " + winId.size());
			for (int i=0; i <winId.size(); i++) {
				widL.add(winIds.next());
			}
			 driver.switchTo().window(widL.get(1));  
			
			return "Pass";
		} catch (Throwable t) {
			System.out.println("Object " + object + " Not found "+ t.getMessage());
			return "Fail" + t.getMessage();
		}
	}
	
	public static String SwitchtoParent(){
		System.out.println("This is SwitchtoParent method");
		try {
			
			Thread.sleep(1000);			
			// Window Handles when more than 1 Browser are present
			Set<String> winId  = driver.getWindowHandles();			
			Iterator<String> winIds = winId.iterator();
			List<String> widL = new ArrayList();
			System.out.println("Size of the window hanldes(ID) is " + winId.size());
			for (int i=0; i <winId.size(); i++) {
				widL.add(winIds.next());
			}
			 driver.close();
			 driver.switchTo().window(widL.get(0));  
			
			return "Pass";
		} catch (Throwable t) {
			System.out.println("Object " + object + " Not found "+ t.getMessage());
			return "Fail" + t.getMessage();
		}
	}
	
	public static String doubleClick(){
		System.out.println("This is double click method");
		try {
			WebDriverWait wait = new WebDriverWait(driver, 15);
			wait.until(ExpectedConditions.elementToBeClickable(By.xpath(OBJDESC.getProperty(object))));
			driver.findElement(By.xpath(OBJDESC.getProperty(object))).click();
			driver.findElement(By.xpath(OBJDESC.getProperty(object))).click();
			return "Pass";
		} catch (Throwable t) {
			System.out.println("Object " + object + " Not found "+ t.getMessage());
			return "Fail" + t.getMessage();
		}
	}
	
	public static String LongClick(){
		System.out.println("This is long click method");
		try {
			WebDriverWait wait = new WebDriverWait(driver, 15);
			wait.until(ExpectedConditions.elementToBeClickable(By.xpath(OBJDESC.getProperty(object))));
			driver.findElement(By.xpath(OBJDESC.getProperty(object))).click();
			Thread.sleep(10000);
			return "Pass";
		} catch (Throwable t) {
			System.out.println("Object " + object + " Not found "+ t.getMessage());
			return "Fail" + t.getMessage();
		}
	}
	public static String LongbuttonClick2(){
		System.out.println("This is Long button click2 method");
		try {
			driver.findElement(By.xpath(OBJDESC.getProperty(object))).click();
			driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
			Thread.sleep(15000);
			return "Pass";
		} catch (Throwable t) {
			System.out.println("Object " + object + " Not found "+ t.getMessage());
			return "Fail" + t.getMessage();
		}
	}

	public static String Custom(){
		System.out.println("This is custom method");
		try {
			driver.findElement(By.xpath("//input[@value='Customs']")).click();
			Thread.sleep(2000);
			driver.findElement(By.xpath("//input[@value='Get Customs Data']")).click();
			Thread.sleep(20000);
			return "Pass";
		} catch (Throwable t) {
			System.out.println("Object " + object + " Not found "+ t.getMessage());
			return "Fail" + t.getMessage();
		}

	}
	
	public static void closeApp(){
		System.out.println("Closing app");
		driver.close();
		driver.quit();

	}

	 public static String Query_DB(){
		System.out.println("This is a method to read text and querying database");
		try {
			
			WebElement element = driver.findElement(By.xpath(OBJDESC.getProperty(object)));
			String strng = element.getText();
			strng = strng.replaceAll(" ", "");
			System.out.println(strng);
			vDB_Result = DB_ResultSet(strng);
			System.out.println(vDB_Result);
			 
			return vDB_Result;
		} catch (Throwable t) {
			System.out.println("Object " + object + " Not found "+ t.getMessage());
			return "Fail" + t.getMessage();
		}

	} 
	public static String DB_ResultSet(String param) throws SQLException, ClassNotFoundException {
		System.out.println("This is DB_ResultSet method");
			
		   Class.forName(CONFIG.getProperty("DB_Driver"));
		   myUName  = CONFIG.getProperty("DB_User");
		   myPswd   = CONFIG.getProperty("DB_Pswd");
		   mySQLDB  = CONFIG.getProperty("DB_Server");
		   myCon    = DriverManager.getConnection(mySQLDB, myUName, myPswd);
		   mySt     = myCon.createStatement();
		  
		  	p1 = param; 	
		
		//	myQuery1 = "SELECT LicensePlate +', EventCd: ' + standardizedEventCode AS Event_CD FROM(SELECT  P.LicensePlate, PTE.standardizedEventCode, RANK ( ) OVER ( PARTITION BY P.LicensePlate ORDER BY PTE.eventDateTime DESC) AS Rank,PTE.eventDateTime FROM Parcels.CTS.parcelTrackingEvent PTE INNER JOIN  Parcels.HubApp.Parcel P ON PTE.parcelId = P.parcelId where P.LicensePlate = '"+p2+"') A WHERE A.Rank = 1";   //+ " and cost='" + p1 + "'";
			myQuery1 = data;
	
			PreparedStatement myPst = myCon.prepareStatement(myQuery1);
			myPst.setString(1, p1);
			rs1 = myPst.executeQuery(); 
			
		 //  mySt     = myCon.createStatement();
		 //  rs1      = mySt.executeQuery(myQuery1);
			if (rs1 == null || !rs1.first()) {
			    r1 = "empty";
			} else {
			    //not empty			
		             while(rs1.next()){
		             r1=rs1.getString(1); //Returns column 1
		              } 
			        }
		       myCon.close();
		       System.out.println(r1); 
		       return r1;
		  	      
	 }  
	
}


